%%
%% This is file `changebar.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% changebar.dtx  (with options: `package')
%% This is a generated file.
%% 
%% Copyright (C) 1994-2005 Johannes Braams,  All rights reserved.
%% 
%% This file was generated from file(s) of the changebar package.
%% --------------------------------------------------------------
%% 
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%% 
%% The Current Maintainer of this work is Johannes Braams.
%% 
%% This file may only be distributed together with a copy of the
%% changebar package. You may however distribute thechangebar package
%% without such generated files.
%% 
%% The list of all files belonging to the changebar package is
%% given in the file `manifest.txt.
%% 
%% The list of derived (unpacked) files belonging to the distribution
%% and covered by LPPL is defined by the unpacking scripts (with
%% extension .ins) which are part of the distribution.
%% Copyright 1994-2005 (C) Johannes Braams.  All rights reserved.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{changebar}
           [2005/09/18 v3.5c Indicate changes with marginal bars]
\def\cb@maxpoint{80}
\def\cb@minpoint{1}
\def\cb@nil{0}
\newcount\cb@nextpoint
\cb@nextpoint=\cb@minpoint
\newcount\cb@topleft
\newcount\cb@topright
\newcount\cb@botleft
\newcount\cb@botright
\newcount\cb@cnta
\newcount\cb@cntb
\newdimen\cb@dima
\newdimen\cb@curbarwd
\newcount\cb@page
\newcount\cb@pagecount
\cb@pagecount=0
\def\cb@barsplace{0}
\newif\if@cb@trace
\newif\if@cb@firstcolumn
\def\cb@pdfxy#1#2#3#4#5{}
\newdimen\cb@odd@left
\newdimen\cb@odd@right
\newdimen\cb@even@left
\newdimen\cb@even@right
\def\cb@positions{%
  \global\cb@odd@left=\hoffset
  \global\cb@even@left\cb@odd@left
  \global\advance\cb@odd@left by \oddsidemargin
  \global\cb@odd@right\cb@odd@left
  \global\advance\cb@odd@right by \textwidth
  \global\advance\cb@odd@right by \changebarsep
  \global\advance\cb@odd@right by 0.5\changebarwidth
  \global\advance\cb@odd@left by -\changebarsep
  \global\advance\cb@odd@left by -0.5\changebarwidth
  \if@twoside
    \global\advance\cb@even@left by \evensidemargin
    \global\cb@even@right\cb@even@left
    \global\advance\cb@even@left by -\changebarsep
    \global\advance\cb@even@left by -0.5\changebarwidth
    \global\advance\cb@even@right by \textwidth
    \global\advance\cb@even@right by \changebarsep
    \global\advance\cb@even@right by 0.5\changebarwidth
  \else
    \global\let\cb@even@left\cb@odd@left
    \global\let\cb@even@right\cb@odd@right
  \fi
  }
{\catcode`\p=12\catcode`\t=12 \gdef\cb@removedim#1pt{#1}}
\DeclareOption{DVItoLN03}{\global\chardef\cb@driver@setup=0\relax}
\DeclareOption{dvitoln03}{\global\chardef\cb@driver@setup=0\relax}
\DeclareOption{DVItoPS}{\global\chardef\cb@driver@setup=1\relax}
\DeclareOption{dvitops}{\global\chardef\cb@driver@setup=1\relax}
\DeclareOption{DVIps}{\global\chardef\cb@driver@setup=2\relax}
\DeclareOption{dvips}{\global\chardef\cb@driver@setup=2\relax}
\DeclareOption{emTeX}{\global\chardef\cb@driver@setup=3\relax}
\DeclareOption{emtex}{\global\chardef\cb@driver@setup=3\relax}
\DeclareOption{textures}{\global\chardef\cb@driver@setup=4\relax}
\DeclareOption{Textures}{\global\chardef\cb@driver@setup=4\relax}
\DeclareOption{VTeX}{\global\chardef\cb@driver@setup=5\relax}
\DeclareOption{vtex}{\global\chardef\cb@driver@setup=5\relax}
\DeclareOption{PDFTeX}{\cb@pdftexcheck}
\DeclareOption{pdftex}{\cb@pdftexcheck}
\def\cb@pdftexcheck{%
  \ifx\pdfsavepos\@undefined\cb@pdftexerror
  \else\ifx\pdfoutput\@undefined\cb@pdftexerror
  \else\ifnum\pdfoutput>0
    \global\chardef\cb@driver@setup=6\relax
    \ifx\cb@writexy\@undefined
      \newwrite\cb@writexy
      \newread\cb@readxy
      \immediate\openout\cb@writexy=\jobname.cb2\relax
    \fi
    \gdef\cb@pdfxy##1##2##3##4##5{%
      \immediate\write\cb@writexy{##1.##2p##3,##4,##5}%
      \expandafter\gdef\csname cb@##1.##2\endcsname{##3,##4,##5}}
  \else\cb@pdftexerror\fi\fi\fi}
\def\cb@pdftexerror{\PackageError
      {changebar}%
      {PDFTeX option cannot be used}%
      {You are using a LaTeX run which does not generate PDF\MessageBreak
        or you are using a very old version of PDFTeX}}
\DeclareOption{outerbars}{\def\cb@barsplace{1}}
\DeclareOption{innerbars}{\def\cb@barsplace{0}}
\DeclareOption{leftbars}{\def\cb@barsplace{2}}
\DeclareOption{rightbars}{\def\cb@barsplace{3}}
\DeclareOption{traceon}{\@cb@tracetrue}
\DeclareOption{traceoff}{\@cb@tracefalse}
\DeclareOption{tracestacks}{%
  \let\cb@trace@stack\cb@@show@stack
  \def\cb@trace@push#1{\cb@trace{%
      Pushed point \the\cb@topleft\space on \noexpand#1: #1}}%
  \def\cb@trace@pop#1{\cb@trace{%
      Popped point \the\cb@topleft\space from \noexpand#1: #1}}%
  }
\DeclareOption{grey}{%
  \def\cb@ps@color{\thechangebargrey\space 100 div setgray}}
\DeclareOption{color}{%
  \def\cb@ps@color{\expandafter\c@lor@to@ps\cb@current@color\@@}%
  \def\cb@color@pkg{color}}
\DeclareOption{xcolor}{%
  \def\cb@ps@color{\expandafter\c@lor@to@ps\cb@current@color\@@}%
  \def\cb@color@pkg{xcolor}}
\DeclareOption*{\OptionNotUsed\PackageError
      {changebar}%
      {Unrecognised option `\CurrentOption'\MessageBreak
        known options are dvitoln03, dvitops, dvips,\MessageBreak
        emtex, textures, pdftex and vtex,
        grey, color, xcolor,\MessageBreak
        outerbars, innerbars, leftbars and rightbars}}
\ifx\VTeXversion\@undefined
  \ifx\pdfoutput\@undefined
    \ExecuteOptions{innerbars,traceoff,dvips,grey}
  \else
    \ifnum\pdfoutput>0
      \ExecuteOptions{innerbars,traceoff,pdftex,grey}
    \else
      \ExecuteOptions{innerbars,traceoff,dvips,grey}
    \fi
  \fi
\else
  \ExecuteOptions{innerbars,traceoff,vtex,grey}
\fi
\InputIfFileExists{changebar.cfg}{}{}
\def\cb@@show@stack#1{%
  \cb@trace{%
    stack status at #1:\MessageBreak
    current stack: \cb@currentstack\MessageBreak
    \@spaces end stack: \cb@endstack\MessageBreak
    \space\space begin stack: \cb@beginstack\MessageBreak
    history stack: \cb@historystack
    }}
\let\cb@trace@stack\@gobble
\let\cb@trace@push\@gobble
\let\cb@trace@pop\@gobble
\ProcessOptions\relax
\ifx\pdfsavepos\@undefined
\else
  \ifx\pdfoutput\@undefined
  \else
    \ifnum\pdfoutput>0
      \global\chardef\cb@driver@setup=6\relax
    \fi
  \fi
\fi
\newcommand{\cb@trace}[1]{%
  \if@cb@trace
    \GenericWarning
      {(changebar)\@spaces\@spaces}%
      {Package changebar: #1\@gobble}%
  \fi
  }
\if@compatibility
  \def\driver#1{%
    \bgroup\edef\next{\def\noexpand\tempa{#1}}%
      \uppercase\expandafter{\next}%
      \def\LN{DVITOLN03}%
      \def\DVItoPS{DVITOPS}%
      \def\DVIPS{DVIPS}%
      \def\emTeX{EMTEX}%
      \def\Textures{TEXTURES}%
      \def\VTeX{VTEX}%
      \def\pdfTeX{PDFTEX}
      \global\chardef\cb@driver@setup=0\relax
      \ifx\tempa\LN       \global\chardef\cb@driver@setup=0\fi
      \ifx\tempa\DVItoPS  \global\chardef\cb@driver@setup=1\fi
      \ifx\tempa\DVIPS    \global\chardef\cb@driver@setup=2\fi
      \ifx\tempa\emTeX    \global\chardef\cb@driver@setup=3\fi
      \ifx\tempa\Textures \global\chardef\cb@driver@setup=4\fi
      \ifx\tempa\VTeX     \global\chardef\cb@driver@setup=5\fi
      \ifx\tempa\pdfTeX   \cb@pdftexcheck\fi
    \egroup}
  {\def\do{\noexpand\do\noexpand}
    \xdef\@preamblecmds{\@preamblecmds \do\driver}
    }
\fi
\def\cb@trace@defpoint#1#2{%
  \cb@trace{%
    defining point \the#1 at position \the#2
    \MessageBreak
    cb@pagecount: \the\cb@pagecount; page \thepage}}
\def\cb@trace@connect#1#2#3{%
  \cb@trace{%
    connecting points \the#1 and \the#2; barwidth: \the#3
    \MessageBreak
    cb@pagecount: \the\cb@pagecount; page \thepage}}
\def\cb@setup@specials{%
\ifcase\cb@driver@setup
  \def\cb@defpoint##1##2{%
    \special{ln03:defpoint \the##1(\the##2,)}%
    \cb@trace@defpoint##1##2}
  \def\cb@connect##1##2##3{%
    \special{ln03:connect \the##1\space\space \the##2\space \the##3}%
    \cb@trace@connect##1##2##3}
  \def\cb@resetpoints{%
    \special{ln03:resetpoints \cb@minpoint \space\cb@maxpoint}}
\or
  \def\cb@defpoint##1##2{%
    \special{dvitops: inline
                \expandafter\cb@removedim\the##2\space 6.5536 mul\space
                /CBarX\the##1\space exch def currentpoint exch pop
                /CBarY\the##1\space exch def}%
    \cb@trace@defpoint##1##2}
  \def\cb@connect##1##2##3{%
    \special{dvitops: inline
                gsave \cb@ps@color\space
                \expandafter\cb@removedim\the##3\space 6.5536 mul\space
                CBarX\the##1\space\space CBarY\the##1\space\space moveto
                CBarX\the##2\space\space CBarY\the##2\space\space lineto
                stroke grestore}%
    \cb@trace@connect##1##2##3}
  \let\cb@resetpoints\relax
\or
  \def\cb@defpoint##1##2{%
     \special{ps:
                \expandafter\cb@removedim\the##2\space
                Resolution\space mul\space 72.27\space div\space
                /CBarX\the##1\space exch def currentpoint exch pop
                /CBarY\the##1\space exch def}%
    \cb@trace@defpoint##1##2}
  \def\cb@connect##1##2##3{%
    \special{ps:
                gsave \cb@ps@color\space
                \expandafter\cb@removedim\the##3\space
                Resolution\space mul\space 72.27\space div\space
                setlinewidth
                CBarX\the##1\space\space CBarY\the##1\space\space moveto
                CBarX\the##2\space\space CBarY\the##2\space\space lineto
                stroke grestore}%
    \cb@trace@connect##1##2##3}
  \let\cb@resetpoints\relax
\or
  \def\cb@defpoint##1##2{%
    \special{em:point \the##1,\the##2}%
    \cb@trace@defpoint##1##2}
  \def\cb@connect##1##2##3{%
    \special{em:line \the##1,\the##2,\the##3}%
    \cb@trace@connect##1##2##3}
  \let\cb@resetpoints\relax
\or
  \def\cb@defpoint##1##2{%
    \special{postscript 0 0 transform}% leave [x,y] on the stack
    \special{rawpostscript
                \expandafter\cb@removedim\the##2\space
                /CBarX\the##1\space exch def
                itransform exch pop
                /CBarY\the##1\space exch def}%
    \if@cb@trace\cb@trace@defpoint##1##2\fi}
  \def\cb@connect##1##2##3{%
    \special{rawpostscript
                gsave 1 setlinecap \cb@ps@color\space
                \expandafter\cb@removedim\the##3\space
                setlinewidth
                CBarX\the##1\space\space CBarY\the##1\space\space moveto
                CBarX\the##2\space\space CBarY\the##2\space\space lineto
                stroke grestore}%
    \if@cb@trace\cb@trace@connect##1##2##3\fi}
  \let\cb@resetpoints\relax
\or
  \def\cb@defpoint##1##2{%
    \special{pS:
                \expandafter\cb@removedim\the##2\space
                Resolution\space mul\space 72.27\space div\space
                /CBarX\the##1\space exch def currentpoint exch pop
                /CBarY\the##1\space exch def}%
    \cb@trace@defpoint##1##2}
  \def\cb@connect##1##2##3{%
    \special{pS:
                gsave \cb@ps@color\space
                \expandafter\cb@removedim\the##3\space
                Resolution\space mul\space 72.27\space div\space
                setlinewidth
                CBarX\the##1\space\space CBarY\the##1\space\space moveto
                CBarX\the##2\space\space CBarY\the##2\space\space lineto
                stroke grestore}%
    \cb@trace@connect##1##2##3}
  \let\cb@resetpoints\relax
\or
  \immediate\closeout\cb@writexy
  \immediate\openin\cb@readxy=\jobname.cb2\relax
  \def\cb@pdfpoints{}
  \def\cb@pdfpagenr{0}
  \def\cb@findpdfpoint##1##2{%
      \edef\cb@temp
        {\noexpand\cb@@findpdfpoint{\the##1}{\the##2}}%
      \cb@temp
  }
  \def\cb@@findpdfpoint##1##2{%
    \ifnum##2<\cb@pdfpagenr\relax\else
      \cb@pdfreadxy{##2}%
    \fi
    \let\cb@pdfx\relax
    \ifx\cb@pdfpoints\@empty\else
      \ifnum##2<0\relax
      \else
        \edef\cb@temp{\noexpand\cb@pdffind{##1}{##2}\cb@pdfpoints\relax{}}%
        \cb@temp
      \fi
    \fi
  }
  \def\cb@pdffind##1##2##3.##4p##5,##6,##7pt##8\relax##9{%
    \def\cb@next{\cb@pdffind{##1}{##2}##8\relax{##9##3.##4p##5,##6,##7pt}}%
    \ifnum ##1=##3
      \ifnum ##2=##4
        \def\cb@pdfx{##5sp}%
        \def\cb@pdfy{##6sp}%
        \def\cb@pdfz{##7pt}%
        \let\cb@next\relax
        \gdef\cb@pdfpoints{##9##8}%
      \fi
    \fi
    \ifx\relax##8\relax
      \let\cb@next\relax
    \fi
    \cb@next
  }%
  \def\cb@pdfreadxy##1{%
    \let\cb@next\relax
    \ifeof\cb@readxy
      \global\let\cb@pdfpagenr\cb@maxpoint
    \else
      {\endlinechar=-1\read\cb@readxy to\cb@temp
        \ifx\cb@temp\@empty\else
          \expandafter\cb@pdfparsexy\cb@temp
          \ifnum\cb@pdfpg<0\else
            \xdef\cb@pdfpoints{\cb@pdfpoints\cb@temp}%
            \cb@trace{PDFpoints=\cb@pdfpoints}%
            \global\let\cb@pdfpagenr\cb@pdfpg
          \fi
          \ifnum\cb@pdfpg>##1\else
            \global\def\cb@next{\cb@pdfreadxy{##1}}%
          \fi
        \fi
      }%
    \fi
    \cb@next
  }%
  \def\cb@pdfparsexy##1.##2p##3,##4,##5pt{%
    \def\cb@pdfpg{##2}}%
  \def\cb@defpoint##1##2{%
    \if@filesw
      \begingroup
        \edef\point{{\the##1}{\the\cb@pagecount}}%
        \let\the=\z@
        \pdfsavepos
        \edef\cb@temp{\write\@auxout
          {\string\cb@pdfxy\point
            {\the\pdflastxpos}{\the\pdflastypos}{\the##2}}}%
        \cb@temp
      \endgroup
    \fi
    \cb@trace@defpoint##1##2%
  }%
  \def\cb@cvtpct##1{%
    \ifnum##1<0 0\else
    \ifnum##1>99 1\else
    \ifnum##1<10 0.0\the##1\else
    0.\the##1\fi\fi\fi}
  \def\cb@connect##1##2##3{%
    \cb@findpdfpoint{##1}\cb@pagecount
    \ifx\cb@pdfx\relax\cb@rerun
    \else
      \let\cb@pdftopy\cb@pdfy
      \cb@findpdfpoint{##2}\cb@pagecount
      \ifx\cb@pdfx\relax\cb@rerun
      \else
         \begingroup
           \cb@dima=\cb@pdfz
           \advance\cb@dima by-\cb@pdfx
           \advance\cb@dima by1in
           \cb@dima=0.996264009963\cb@dima\relax
           \ifx\cb@current@color\@undefined
             \def\cb@temp{\cb@cvtpct\c@changebargrey}%
             \pdfliteral{q \cb@temp\space g \cb@temp\space G}%
           \else
             \pdfliteral{q \cb@current@color}%
           \fi
           \edef\cb@temp{\expandafter\cb@removedim\the\cb@dima\space}%
           \cb@dima=\cb@pdftopy
           \advance\cb@dima-\cb@pdfy\relax
           \cb@dima=0.996264009963\cb@dima\relax
           ##3=0.996264009963##3\relax
           \pdfliteral direct{\expandafter\cb@removedim\the##3 w
             \cb@temp 0 m
             \cb@temp \expandafter\cb@removedim\the\cb@dima\space l S Q}%
        \endgroup
        \cb@cntb=##1\relax
        \ifodd\cb@cntb\advance\cb@cntb 1\else\advance\cb@cntb -1\fi
        \cb@findpdfpoint\cb@cntb\cb@pagecount
        \cb@cntb=##2\relax
        \ifodd\cb@cntb\advance\cb@cntb 1\else\advance\cb@cntb -1\fi
        \cb@findpdfpoint\cb@cntb\cb@pagecount
      \fi
    \fi
    \cb@trace@connect##1##2##3%
  }%
  \gdef\cb@checkPdfxy##1##2##3##4##5{%
    \cb@@findpdfpoint{##1}{##2}%
    \ifnum##3=\cb@pdfx\relax
      \ifnum##4=\cb@pdfy\relax
        \ifdim##5=\cb@pdfz\relax
        \else
        \cb@error
        \fi
      \else
        \cb@error
      \fi
    \else
      \cb@error
    \fi
  }
  \def\cb@maxpoint{9999999}
  \let\cb@resetpoints\relax
\or
\else
  \PackageWarning{Changebar}{changebars not supported in unknown setup}
  \def\cb@defpoint##1##2{\cb@trace@defpoint##1##2}
  \def\cb@connect##1##2##3{\cb@trace@connect##1##2##3}
  \let\cb@resetpoints\relax
\fi
\global\let\cb@setup@specials\relax}
\newcommand*{\cbstart}{\@ifnextchar [%]
                        {\cb@start}%
                        {\cb@start[\changebarwidth]}}
\newcommand*{\cbend}{\cb@end}
\newcommand*{\cbdelete}{\@ifnextchar [%]
  {\cb@delete}%
  {\cb@delete[\deletebarwidth]}}
\def\cb@delete[#1]{\vbox to \z@{\vss\cb@start[#1]\vskip #1\cb@end}}
\newenvironment{changebar}%
               {\@ifnextchar [{\cb@start}%
                              {\cb@start[\changebarwidth]}}%
               {\cb@end}
\newcommand*{\nochangebars}{%
  \def\cb@start[##1]{}%
  \def\cb@delete[##1]{}%
  \let\cb@end\relax}
\newlength{\changebarwidth}
\setlength{\changebarwidth}{2pt}
\newlength{\deletebarwidth}
\setlength{\deletebarwidth}{4pt}
\newlength{\changebarsep}
\setlength{\changebarsep}{30pt}
\newcounter{changebargrey}
\setcounter{changebargrey}{65}
\@ifpackagewith{changebar}{\csname cb@color@pkg\endcsname}{%
    \RequirePackage{\cb@color@pkg}%
  \DeclareRobustCommand\cbcolor{%
    \@ifnextchar[\@undeclaredcbcolor\@declaredcbcolor}
  \def\@undeclaredcbcolor[#1]#2{%
    \begingroup
      \color[#1]{#2}%
      \global\let\cb@current@color\current@color
    \endgroup
    \ignorespaces
    }
  \def\@declaredcbcolor#1{%
    \begingroup
      \color{#1}%
      \global\let\cb@current@color\current@color
    \endgroup
    \ignorespaces}%
  }{%
  \def\cbcolor{\@ifnextchar[%]
    \@@cbcolor\@cbcolor}%
  \def\@@cbcolor[#1]#2{\cb@colwarn\def\@@cbcolor[##1]##2{}}%
  \def\@cbcolor#1{\cb@colwarn\def\@cbcolor##1{}}%
  \def\cb@colwarn{\PackageWarning{Changebar}%
    {You didn't specify the option `color';\MessageBreak
      your command \string\cbcolor\space will be ignored}}%
  }
\def\cb@start[#1]{%
  \cb@topleft=\cb@nextpoint
  \cb@curbarwd#1\relax
  \cb@push\cb@currentstack
  \cb@checkpage\z@
  \cb@cnta\cb@pagecount
  \cb@pagecount\cb@page\advance\cb@pagecount\m@ne
  \ifvmode
    \cb@setBeginPoints
  \else
    \vbox to \z@{%
      \vskip -\ht\strutbox
      \cb@setBeginPoints
      \vskip \ht\strutbox}%
  \fi
  \cb@pagecount\cb@cnta
  \cb@advancePoint}
\def\cb@advancePoint{%
  \global\advance\cb@nextpoint by 4\relax
  \ifnum\cb@nextpoint>\cb@maxpoint
    \global\cb@nextpoint=\cb@minpoint\relax
  \fi}
\def\cb@end{%
  \cb@trace@stack{end of bar on page \the\c@page}%
  \cb@pop\cb@currentstack
  \ifnum\cb@topleft=\cb@nil
    \PackageWarning{Changebar}%
      {Badly nested changebars; Expect erroneous results}%
  \else
    \cb@checkpage\thr@@
    \cb@cnta\cb@pagecount
    \cb@pagecount\cb@page\advance\cb@pagecount\m@ne
    \ifvmode
      \cb@setEndPoints
    \else
      \vadjust{\cb@setEndPoints}%
    \fi
    \cb@pagecount\cb@cnta
  \fi
  \ignorespaces}
\def\cb@checkpage#1{%
  \cb@cnta\cb@topleft\relax
  \advance\cb@cnta by #1\relax
  \cb@cntb\cb@page\relax
  \cb@dima\cb@curbarwd\relax
  \cb@pop\cb@historystack
  \ifnum\cb@topleft=\cb@nil
  \else
    \cb@FindPageNum
    \ifnum\cb@topleft>\cb@maxpoint\else
      \cb@cntb\cb@page
    \fi
    \@whilenum\cb@topleft>\cb@nil\do{%
      \cb@push\cb@historystack
      \cb@pop\cb@tempstack}%
  \fi
  \advance\cb@cnta by -#1\relax
  \cb@topleft\cb@cnta\relax
  \cb@page\cb@cntb\relax
  \cb@curbarwd\cb@dima\relax
  }
\def\cb@FindPageNum{%
  \ifnum\cb@topleft=\cb@cnta
  \else
    \cb@push\cb@tempstack
    \cb@pop\cb@historystack
    \ifnum\cb@topleft=\cb@nil
      \cb@trace{Ran out of history stack, new changebar?}%
      \cb@topleft\cb@maxpoint\advance\cb@topleft\@ne
    \else
      \expandafter\expandafter\expandafter\cb@FindPageNum
    \fi
  \fi
  }%
\def\cb@setBeginPoints{%
  \cb@topright=\cb@topleft\advance\cb@topright by\@ne
  \cb@cntb=\cb@pagecount
  \divide\cb@cntb by\tw@
  \ifodd\cb@cntb
    \cb@defpoint\cb@topleft\cb@even@left
    \cb@defpoint\cb@topright\cb@even@right
  \else
    \cb@defpoint\cb@topleft\cb@odd@left
    \cb@defpoint\cb@topright\cb@odd@right
  \fi
  \cb@writeAux\cb@topleft
  }
\def\cb@setEndPoints{%
  \cb@topright=\cb@topleft\advance\cb@topright by\@ne
  \cb@botleft=\cb@topleft\advance\cb@botleft by\tw@
  \cb@botright=\cb@topleft\advance\cb@botright by\thr@@
   \cb@cntb=\cb@pagecount
  \divide\cb@cntb by\tw@
  \ifodd\cb@cntb
    \cb@defpoint\cb@botleft\cb@even@left
    \cb@defpoint\cb@botright\cb@even@right
  \else
    \cb@defpoint\cb@botleft\cb@odd@left
    \cb@defpoint\cb@botright\cb@odd@right
  \fi
  \cb@writeAux\cb@botright
  \edef\cb@leftbar{%
    \noexpand\cb@connect{\cb@topleft}{\cb@botleft}{\cb@curbarwd}}%
  \edef\cb@rightbar{%
    \noexpand\cb@connect{\cb@topright}{\cb@botright}{\cb@curbarwd}}%
  \if@twocolumn
     \ifodd\cb@pagecount\cb@rightbar\else\cb@leftbar\fi
  \else
    \ifcase\cb@barsplace
      \ifodd\cb@cntb
        \cb@rightbar
      \else
        \if@twoside\cb@leftbar\else\cb@rightbar\fi
      \fi
    \or
      \ifodd\cb@cntb
        \cb@leftbar
      \else
        \if@twoside\cb@rightbar\else\cb@leftbar\fi
      \fi
    \or
      \cb@leftbar
    \or
      \cb@rightbar
    \fi
  \fi
  }%
\def\cb@writeAux#1{%
  \if@filesw
    \begingroup
      \edef\point{\the#1}%
      \edef\level{\the\cb@curbarwd}%
      \let\the=\z@
      \edef\cb@temp{\write\@auxout
        {\string\cb@barpoint{\point}{\the\cb@pagecount}{\level}}}%
      \cb@temp
    \endgroup
  \fi}
\newif\if@cb@pagejump
\def\cb@pagejumplst{-1}
\def\cb@nextpagejump{-1}
\def\cb@pagejump#1{\xdef\cb@pagejumplst{\cb@pagejumplst,#1}}
\def\cb@writepagejump#1{
  \cb@cntb=\cb@pagecount
  \advance\cb@cntb by#1\relax
  \global\setbox\@leftcolumn\vbox to\@colht{%
    \edef\cb@temp{\write\@auxout{\string\cb@pagejump{\the\cb@cntb}}}%
    \cb@temp
    \dimen@ \dp\@leftcolumn
    \unvbox \@leftcolumn
    \vskip -\dimen@
  }%
}
\def\cb@poppagejump#1,#2\relax{%
  \gdef\cb@nextpagejump{#1}%
  \gdef\cb@pagejumplst{#2}}
\def\cb@checkpagecount{%
  \if@twocolumn
    \if@firstcolumn
      \ifodd\cb@pagecount\global\advance\cb@pagecount by\@ne\fi
    \fi
  \else
    \ifodd\cb@pagecount\global\advance\cb@pagecount by\@ne\fi
  \fi
  \if@twoside
    \cb@cntb=\cb@pagecount
    \divide\cb@cntb by\tw@
    \advance\cb@cntb by-\c@page
    \ifodd\cb@cntb
      \if@twocolumn
        \if@firstcolumn
          \@whilenum\cb@pagecount>\cb@nextpagejump\do{%
            \expandafter\cb@poppagejump\cb@pagejumplst\relax}%
          \ifnum\cb@pagecount=\cb@nextpagejump
            \cb@trace{Page jump: \string\cb@pagecount=\the\cb@pagecount}
            \global\advance\cb@pagecount by\tw@
            \global\@cb@pagejumptrue
          \else
            \global\@cb@pagejumpfalse
          \fi
        \else
          \if@cb@pagejump
            \cb@writepagejump{-3}%
          \fi
        \fi
      \fi
    \else
      \if@twocolumn
        \if@firstcolumn
          \global\advance\cb@pagecount by\tw@
          \global\@cb@pagejumpfalse
        \else
          \if@cb@pagejump
            \cb@trace{Page jump annulled, %
                      \string\cb@pagecount=\the\cb@pagecount}
          \else
            \cb@writepagejump{-1}%
            \global\advance\cb@pagecount by\tw@
            \cb@rerun
          \fi
        \fi
      \else
        \global\advance\cb@pagecount by\tw@
      \fi
    \fi
  \fi
}
\let\ltx@makecol\@makecol
\def\cb@makecol{%
  \if@twocolumn
    \cb@trace{Twocolumn: \if@firstcolumn Left \else Right \fi column}%
  \fi
  \cb@trace@stack{before makecol, page \the\c@page,
                  \string\cb@pagecount=\the\cb@pagecount}%
  \let\cb@writeAux\@gobble
  \cb@checkpagecount
  \setbox\@cclv \vbox{%
    \cb@resetpoints
    \cb@startSpanBars
    \unvbox\@cclv
    \boxmaxdepth\maxdepth}%
  \global\advance\cb@pagecount by\@ne
  \cb@buildstack\cb@processActive
  \ltx@makecol
   \if@twocolumn
     \global\setbox\@outputbox \vbox to\@colht {%
       \if@firstcolumn\write\@auxout{\string\@cb@firstcolumntrue}%
       \else\write\@auxout{\string\@cb@firstcolumnfalse}%
       \fi
       \dimen@ \dp\@outputbox
       \unvbox \@outputbox
       \vskip -\dimen@
       }%
   \fi
  \cb@trace@stack{after makecol, page \the\c@page,
                  \string\cb@pagecount=\the\cb@pagecount}%
  }
\let\@makecol\cb@makecol
\let\ltx@vtryfc\@vtryfc
\def\cb@vtryfc#1{%
  \cb@trace{In vtryfc, page \the\c@page,
                  \string\cb@pagecount=\the\cb@pagecount}%
  \let\cb@writeAux\@gobble
  \cb@checkpagecount
  \ltx@vtryfc{#1}%
  \if@twocolumn
    \global\setbox\@outputbox \vbox to\@colht{%
      \if@firstcolumn\write\@auxout{\string\@cb@firstcolumntrue}%
      \else\write\@auxout{\string\@cb@firstcolumnfalse}%
      \fi
      \unvbox\@outputbox
      \boxmaxdepth\maxdepth
    }%
  \fi
  \global\advance\cb@pagecount by \@ne
}
\let\@vtryfc\cb@vtryfc
\def\cb@processActive{%
  \cb@pop\cb@endstack
  \ifnum\cb@topleft=\cb@nil
  \else
    \setbox\@cclv\vbox{%
      \unvbox\@cclv
      \boxmaxdepth\maxdepth
      \advance\cb@pagecount by -1\relax
      \cb@setEndPoints}%
    \cb@push\cb@historystack
    \cb@push\cb@beginstack
    \expandafter\cb@processActive
  \fi}
\def\cb@startSpanBars{%
  \cb@pop\cb@beginstack
  \ifnum\cb@topleft=\cb@nil
  \else
    \cb@setBeginPoints
    \cb@trace@stack{after StartSpanBars, page \the\c@page}%
    \expandafter\cb@startSpanBars
  \fi
  }
\def\cb@buildstack{%
  \cb@initstack\cb@endstack
  \cb@pushNextActive}
\def\cb@pushNextActive{%
  \cb@pop\cb@historystack
  \ifnum\cb@topleft=\cb@nil
  \else
    \ifnum\cb@page>\cb@pagecount
      \cb@push\cb@historystack
    \else
      \ifodd\cb@topleft
        \cb@push\cb@endstack
      \else
        \cb@pop\cb@endstack
      \fi
      \expandafter\expandafter\expandafter\cb@pushNextActive
    \fi
  \fi}
\def\cb@initstack#1{\xdef#1{}}
\cb@initstack\cb@historystack
\newwrite\cb@write
\newread\cb@read
\immediate\openout\cb@write=\jobname.cb\relax
\cb@initstack\cb@endstack
\cb@initstack\cb@beginstack
\cb@initstack\cb@tempstack
\cb@initstack\cb@currentstack
\def\cb@thehistorystack{\cb@historystack}
\def\cb@pop#1{%
  \ifx #1\@empty
    \def\cb@temp{#1}%
    \ifx\cb@temp\cb@thehistorystack
      \ifeof\cb@read
      \else
        {\endlinechar=-1\read\cb@read to\cb@temp
         \xdef\cb@historystack{\cb@historystack\cb@temp}%
        }%
      \fi
    \fi
  \fi
  \ifx#1\@empty
    \global\cb@topleft\cb@nil
    \global\cb@page\z@\relax
  \else
    \expandafter\cb@carcdr#1e#1%
  \fi
  \cb@trace@pop{#1}}
\def\cb@carcdr#1n#2p#3l#4e#5{%
  \global\cb@topleft#1\relax
  \global\cb@page#2\relax
  \global\cb@curbarwd#3\relax
  \xdef#5{#4}}
\def\cb@push#1{%
  \xdef#1{\the\cb@topleft n\the\cb@page p\the\cb@curbarwd l#1}%
  \cb@trace@push{#1}}

\def\cb@barpoint#1#2#3{\cb@cnta=#2
  \if@cb@firstcolumn\advance\cb@cnta by\m@ne\fi
  \immediate\write\cb@write{#1n\the\cb@cnta p#3l}}
\AtBeginDocument{%
  \cb@setup@specials
  \cb@pagejump{999999999,}%
  \cb@positions
  \cb@trace{%
    Odd left : \the\cb@odd@left\space
    Odd right : \the\cb@odd@right\MessageBreak
    Even left: \the\cb@even@left\space
    Even right: \the\cb@even@right
    }%
  \immediate\closeout\cb@write
  \immediate\openin\cb@read=\jobname.cb}
\AtEndDocument{%
  \clearpage
  \cb@initstack\cb@historystack
  \immediate\closein\cb@read
  \immediate\openin\cb@read=\jobname.cb%
  \ifx\cb@readxy\@undefined
  \else
    \immediate\closein\cb@readxy
    \immediate\openin\cb@readxy=\jobname.cb2%
    \def\cb@pdfpoints{}%
    \def\cb@pdfpagenr{0}%
  \fi
  \@cb@firstcolumnfalse
  \cb@checkrerun
  \let\cb@pdfxy\cb@checkPdfxy
  \let\cb@pagejump\@gobble
  \let\cb@barpoint\cb@checkHistory}
\def\cb@checkHistory#1#2#3{%
  \cb@pop\cb@historystack
  \ifnum #1=\cb@topleft\relax
    \cb@cnta=#2
    \if@cb@firstcolumn\advance\cb@cnta by\m@ne\fi
    \ifnum \cb@cnta=\cb@page\relax
    \else
      \cb@error
    \fi
  \else
    \cb@error
  \fi}
\def\cb@checkPdfxy#1#2#3#4#5{}
\def\cb@rerun{%
  \global\let\cb@checkrerun\cb@error}
\let\cb@checkrerun\relax
\def\cb@error{%
  \PackageWarning{Changebar}%
    {Changebar info has changed.\MessageBreak
     Rerun to get the bars right}
  \gdef\cb@checkHistory##1##2##3{}%
  \let\cb@barpoint\cb@checkHistory
  \gdef\cb@checkPdfxy##1##2##3##4##5{}%
  \let\cb@pdfxy\cb@checkPdfxy}
\let\ltx@end@float\end@float
\def\cb@end@float{%
  \cb@trace@stack{end float on page \the\c@page}%
  \cb@pop\cb@currentstack
  \ifnum\cb@topleft=\cb@nil
  \else
    \cb@push\cb@currentstack
    \global\cb@curbarwd=\cb@curbarwd
    \@endfloatbox
    \global\setbox\@currbox
      \color@vbox
      \normalcolor
      \vbox\bgroup\cb@start[\cb@curbarwd]\unvbox\@currbox\cb@end
  \fi
  \ltx@end@float}
\let\end@float\cb@end@float
\let\flt@float@end\float@end
\def\float@end{%
  \cb@trace@stack{end float on page \the\c@page}%
  \cb@pop\cb@currentstack
  \ifnum\cb@topleft=\cb@nil
  \else
    \cb@push\cb@currentstack
    \global\cb@curbarwd\cb@curbarwd
    \@endfloatbox
    \global\setbox\@currbox
      \color@vbox
      \normalcolor
      \vbox\bgroup\cb@start[\cb@curbarwd]\unvbox\@currbox\cb@end
  \fi
  \let\end@float\ltx@end@float
  \flt@float@end
  }
\let\ltx@end@dblfloat\end@dblfloat
\def\cb@end@dblfloat{%
  \if@twocolumn
    \cb@trace@stack{end dblfloat on page \the\c@page}%
    \cb@pop\cb@currentstack
    \ifnum\cb@topleft=\cb@nil
    \else
      \cb@push\cb@currentstack
      \global\cb@curbarwd=\cb@curbarwd
      \@endfloatbox
      \global\setbox\@currbox
        \color@vbox
        \normalcolor
        \vbox\bgroup\cb@start[\cb@curbarwd]\unvbox\@currbox\cb@end
    \fi
    \@endfloatbox
    \ifnum\@floatpenalty <\z@
      \@largefloatcheck
      \@cons\@dbldeferlist\@currbox
    \fi
    \ifnum \@floatpenalty =-\@Mii \@Esphack\fi
  \else
    \end@float
  \fi}
\let\end@dblfloat\cb@end@dblfloat
\let\flt@float@dblend\float@dblend
\def\float@dblend{%
  \cb@trace@stack{end dbl float on page \the\c@page}%
  \cb@pop\cb@currentstack
  \ifnum\cb@topleft=\cb@nil
  \else
    \cb@push\cb@currentstack
    \global\cb@curbarwd=\cb@curbarwd
    \@endfloatbox
    \global\setbox\@currbox
      \color@vbox
      \normalcolor
      \vbox\bgroup\cb@start[\cb@curbarwd]\unvbox\@currbox\cb@end
  \fi
  \let\end@dblfloat\ltx@end@dblfloat
  \flt@float@dblend
  }
\let\ltx@footnotetext\@footnotetext
\long\def\cb@footnotetext#1{%
  \cb@trace@stack{end footnote on page \the\c@page}%
  \cb@pop\cb@currentstack
  \ifnum\cb@topleft=\cb@nil
    \ltx@footnotetext{#1}%
  \else
    \cb@push\cb@currentstack
    \edef\cb@temp{\the\cb@curbarwd}%
    \ltx@footnotetext{\cb@start[\cb@temp]#1\cb@end}%
  \fi}
% DOB: line commented out to fix conflict with changes package
%\let\@footnotetext\cb@footnotetext
\let\ltx@mpfootnotetext\@mpfootnotetext
\long\def\cb@mpfootnotetext#1{%
  \cb@pop\cb@currentstack
  \ifnum\cb@topleft=\cb@nil
    \ltx@mpfootnotetext{#1}%
  \else
    \cb@push\cb@currentstack
    \edef\cb@temp{\the\cb@curbarwd}%
    \ltx@mpfootnotetext{\cb@start[\cb@temp]#1\cb@end}%
  \fi}
% DOB: line commented out to fix conflict with changes package
%\let\@mpfootnotetext\cb@mpfootnotetext
\endinput
%%
%% End of file `changebar.sty'.
